package com.bwie.controller;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.pojo.Art;
import com.bwie.pojo.TbOrder;
import com.bwie.pojo.TbRecord;
import com.bwie.pojo.User;
import com.bwie.pojo.vo.ArtDeleteListId;
import com.bwie.service.impl.ArtServiceImpl;
import com.bwie.service.impl.TbOrderServiceImpl;
import com.bwie.service.impl.TbRecordServiceImpl;
import com.bwie.service.impl.TbUserServiceImpl;
import com.bwie.utils.R;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/art/art")
public class ArtController {

    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Autowired
    TbUserServiceImpl tbUserService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    ArtServiceImpl artService;

    @Autowired
    TbRecordServiceImpl tbRecordService;

    @Autowired
    TbOrderServiceImpl tbOrderService;

    /**
     * 图片上传
     *
     * @param file
     * @return
     */
    @PostMapping("/imageup")
    public R ImageUp(@RequestBody MultipartFile file) {
        String s = FileNameUtil.extName(file.getOriginalFilename());
        StorePath storePath = null;
        try {
            storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), s, null);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return R.OK("http://192.168.80.192:8888/" + storePath.getFullPath());
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public R Login(@RequestBody User user) {
        User one = tbUserService.getOne(
                new LambdaQueryWrapper<User>()
                        .eq(User::getUserName, user.getUserName())
                        .eq(User::getUserPass, user.getUserPass())
        );
        if (one == null) {
            return R.ERROR(444, "用户名或密码错误");
        }
        //使用JWTS生成token
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456")
                .claim("userId", one.getUserId())
                .compact();
        stringRedisTemplate.opsForValue().set("token", token);
        return R.OK(token);
    }

    /**
     * 获取美术馆数据
     */
    @GetMapping("/getart")
    public R GetArt() {
        Page<Art> artPage = new Page<>(1,10);
        Page<Art> page = artService.page(artPage);
        for (Art record : page.getRecords()) {
            System.out.println(record);
        }

        List<Art> list = artService.list();
        return R.OK(list);
    }

    /**
     * 美术馆信息修改
     */
    @PostMapping("/upart")
    public R upart(Art art) {
        //http://192.168.80.192:8888/group1/M00/00/02/wKhQwGe5yCuAE3eFAAAf7q-jbh0078.jpg
        artService.updateById(art);
        return R.OK();
    }

    /**
     * 美术馆当天预约记录
     */
    @GetMapping("/jilu")
    public R JiLu() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat.format(date);
        System.out.println(format);
        List<TbRecord> list = tbRecordService.list(new LambdaQueryWrapper<TbRecord>().eq(TbRecord::getRecordDate, format));
        return R.OK(list);
    }

    /**
     * 预约
     */
    @PostMapping("/yvyue")
    public R yvyue(@RequestBody TbOrder tbOrder) {
//        tbRecordService.save(tbOrder);
        Long orderId = tbOrder.getOrderId();
        TbOrder byId = tbOrderService.getById(orderId);
        Integer orderPlaces = Integer.valueOf(tbOrder.getOrderPlaces()) + 1;
        Integer orderPlaces1 = Integer.valueOf(byId.getOrderPlaces());
        if (orderPlaces > orderPlaces1) {
            return R.ERROR(444, "票数不够");
        }
        TbRecord tbRecord = new TbRecord();
        tbRecord.setRecordTime(byId.getOrderTime());
        tbRecord.setRecordNum(String.valueOf(orderPlaces));

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat.format(date);
        tbRecord.setRecordDate(format);

        tbRecordService.save(tbRecord);
        System.out.println(tbOrder);
        return R.OK();
    }

    /**
     * 查询剩余票数
     */
    @PostMapping("/checknum")
    public R CheckNum(@RequestBody TbOrder tbOrder) {
        TbOrder byId = tbOrderService.getById(tbOrder.getOrderId());
        return R.OK(byId);
    }

    /**
     * 美术馆批量删除
     */
    @PostMapping("/deletelist")
    public R DeleteList(@RequestBody List<String> list){
        boolean b = artService.removeByIds(list);
        return R.OK();
    }
}
